/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haiberg.osgi.bundle.utils;

import java.io.File;
import org.osgi.framework.Bundle;

/**
 *
 * @author gyue
 */
public class FileUtils {
    
    public static String getBundleResourcePath(Bundle bundle,String bundleName,String source){
        String basePath = getBundleBasePath(bundle);
        if(basePath != null){
            basePath = basePath.replaceAll("\\\\", "/");
            basePath += "/data/" + bundleName + "/" + source;
        }
        return basePath;
    }
    
    public static String getBundleBasePath(Bundle bundle){
        String bundlePath = bundle.getLocation();
        if(bundlePath.startsWith("file:/")){
            bundlePath = bundlePath.substring(6);
            File bundleFile = new File(bundlePath);
            File bundleBaseDir = bundleFile.getParentFile().getParentFile();
            return bundleBaseDir.getAbsolutePath();
        }
        return null;
    }
}
